<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :append-to-body="true"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="20%"
    @click="closeDialogedit('edit')"
  >
    <el-form label-width="80px" ref="editForm" :model="dataForm" :rules="rules">
      <!-- <el-col :span="24"> -->
      <el-form-item label="角色编码" prop="roleCode">
        <el-input
          size="small"
          v-model="dataForm.roleCode"
          auto-complete="off"
          placeholder="请输入角色编码"
        ></el-input>
      </el-form-item>
      <!-- </el-col> -->

      <el-form-item label="角色名称" prop="roleName">
        <el-input
          size="small"
          v-model="dataForm.roleName"
          auto-complete="off"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="1"
          inactive-value="0"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="closeDialogedit()">取消</el-button>
      <el-button
        size="small"
        type="primary"
        :loading="loading"
        class="title"
        @click="submitFormEdit()"
        >保存</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
//import clampapi from "@/api/mes/basedata/clamp";
import { getRoleInfo, Create, Update } from "@/api/role";
export default {
  components: {},
  props: [],
  data() {
    return {
      btnLoading: false,
      loading: false,
      visible: false,
      dataForm: {
        id: "",
        roleCode: "",
        roleName: "",
        status:"1"
      },
      rules: {
        roleCode: [
          {
            required: true,
            message: "请输入角色编码",
            trigger: "blur",
          },
        ],
        roleName: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    goBack() {
      this.$emit("refresh");
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      console.log(this.dataForm.id);
      this.$nextTick(() => {
        this.$refs["editForm"].resetFields();
        if (this.dataForm.id) {
          //调用数据回显的方法
          getRoleInfo(this.dataForm.id).then((res) => {
            console.log(res.list);
            this.dataForm = res.list;
          });
        }
      });
    },
    submitFormEdit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          const formMethod = !this.dataForm.id ? Create : Update;

          formMethod(this.dataForm)
            .then((res) => {
              console.log(res);
              this.$message({
                message: res.message,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.btnLoading = false;
                  this.visible = false;
                  (this.visible = false), this.$emit("refresh", true);
                },
              });
            })
            .catch((res) => {
              this.btnLoading = false;
            });
        }
      });
    },
    closeDialogedit() {
      console.log("取消");
      this.visible = false;
    },
  },
};
</script>